package cn.sightsnow.zh.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.index.Indexed;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.Duration;
import java.time.LocalDateTime;

@ApiModel(description = "路线信息")
@Data
public class Route {
    @ApiModelProperty("路径ID（可选，不传自动生成）")
    @Id
    private String routeId;

    @ApiModelProperty(value = "路径状态（0-结束，1-运行中）", required = true)
    @NotNull(message = "状态不能为空")
    @Indexed
    private Integer status;

    @ApiModelProperty(value = "起点名称", required = true)
    @NotBlank(message = "起点不能为空")
    private String startPoint;

    @ApiModelProperty(value = "终点名称", required = true)
    @NotBlank(message = "终点不能为空")
    private String endPoint;

    @ApiModelProperty(value = "运送开始时间（自动生成）")
    private LocalDateTime startTime;

    @ApiModelProperty(value = "运送结束时间")
    private LocalDateTime endTime;

    @Transient  // 不持久化到数据库
    @ApiModelProperty(value = "总运行时间（秒）", accessMode = ApiModelProperty.AccessMode.READ_ONLY)
    public Long getDuration() {
        if (startTime == null) return 0L;
        LocalDateTime end = endTime != null ? endTime : LocalDateTime.now();
        return Duration.between(startTime, end).getSeconds();
    }
}
